<!-- 设备保养-保养任务-保养任务清单 -->
<template>
  <div style="width: 100%; height: 100%; margin: 0">
    <iframe v-if="isShow" id="PMS_IFRAME_TASK" width="100%" height="100%" name="ALL_EQUIP_EPM_TASK_LIST" frameborder="0" :src="url" @load="onLoad" />
  </div>
</template>

<script setup lang="ts">
import { getToken } from "@/utils/auth";
import { onMounted, ref } from "vue";
import { useRoute, useRouter } from "vue-router";

// defineOptions({
//   name: "ALL_EQUIP_EPM_TASK_LIST"
// });
const route = useRoute();
const router = useRouter();
const accessToken = getToken().accessToken || "";
const isShow = ref(true);
const url = ref("");

const permission = localStorage.getItem("pmsPermission");

const onLoad = () => {
  const iframeTask = document.getElementById("PMS_IFRAME_TASK") as HTMLIFrameElement;
  iframeTask?.contentWindow?.postMessage(JSON.parse(permission), "*");
};

onMounted(() => {
  isShow.value = true;
  const facCode = localStorage.getItem("facCode");
  if (route.query.fullpath?.includes("?")) {
    url.value = `${import.meta.env.VITE_API_URL_PMS_NEW}/#${route.query.fullpath}&ssoToken=${accessToken}&isHiddenMenu=true&facCode=${facCode}`;
  } else {
    url.value = `${import.meta.env.VITE_API_URL_PMS_NEW}/#/pmJob/list?ssoToken=${accessToken}&isHiddenMenu=true&facCode=${facCode}`;
  }
  window.addEventListener("message", event => {
    // 确保消息来源为pms
    if (event.origin !== import.meta.env.VITE_API_URL_PMS_NEW) return;
    const pmsCodes = ["TBMBtn", "CBMBtn", "UNSBtn", "JobBtn"];
    if (pmsCodes.indexOf(JSON.parse(event.data)?.code) > -1) {
      isShow.value = false;
      setTimeout(() => {
        isShow.value = true;
      }, 0);
    } else if (JSON.parse(event.data)?.code === "PlanList") {
      router.push("/all/equip/epm/plan/list");
    }
  });
});
</script>
